package com.qk.controller;

import com.qk.common.Result;
import com.qk.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
public class UpLoadController {
    /*@PostMapping("/upload")
    public Result upload(String username, Integer age, MultipartFile file) throws IOException {
        log.info("文件上传开始...{}{}{}",username,age,file);
        String name = file.getOriginalFilename();
        String end = name.substring(name.lastIndexOf("."));
        String newFileName= UUID.randomUUID()+"."+end;
        file.transferTo(new File("D:\\images\\"+newFileName));
        return Result.success();
    }*/
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    //传输图片
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws Exception {
        log.info("文件上传开始...{}",image);
        String name = image.getOriginalFilename();
        String end = name.substring(name.lastIndexOf("."));
        String newFileName= UUID.randomUUID()+"."+end;
        String str = aliyunOSSOperator.upload(image.getBytes(),newFileName);
        return Result.success(str);
    }
}
